﻿using System;
using System.Collections.Generic;
using System.Xml;
using System.Text;
using SdmxMl.Helper;

namespace SdmxMl.Structure
{
    /// <summary>
    /// CodeMap describes the equivalence of the codes in the referenced codelist or 
    /// hierarchical codelist indicated in the CodelistRef element of the containing 
    /// CodelistMap to those in the referenced TargetCodelist in the containing CodelistMap. 
    /// The CodeAlias attribute is used to assign an alias code to the equivalence for querying 
    /// the structure set.
    /// </summary>
    public class CodeMapType : StrSetMapItem
    {

        public CodeMapType()
        {
        }

        public CodeMapType(StrSetMapItem item)
        {
            Alias = item.Alias;
            Source = item.Source;
            Target = item.Target;
        }

        public CodeMapType(string alias, string code, string target)
        {
            Alias = alias;
            Source = code;
            Target = target;
        }

        public CodeMapType(XmlNode nodeArtefact, XmlNamespaceManager nsmgr)
        {
            Init();
            Alias = Xmlhelper.GetAtt(nodeArtefact, "CodeAlias");
            Source = Xmlhelper.GetNodeText(nodeArtefact, "./structure:MapCodeRef", nsmgr);
            Target = Xmlhelper.GetNodeText(nodeArtefact, "./structure:MapTargetCodeRef", nsmgr);
        }

        public void WriteXml(XmlTextWriter xw, string prefix, string tag)
        {
            xw.WriteStartElement(prefix, "CodeMap", null);

            Xmlhelper.WriteAttributeIfExist(xw, "CodeAlias", Alias);
            Xmlhelper.WriteElementIfExist(xw, prefix, "MapCodeRef", Source);
            Xmlhelper.WriteElementIfExist(xw, prefix, "MapTargetCodeRef", Target);

            xw.WriteEndElement();
        }
    }

}

